const categoryOptions = [
  { label: '普通场', value: '1' },
  { label: '智能场', value: '2' },
  { label: '机器人场', value: '3' },
  { label: '仿真场', value: '4' }
]
var app = getApp()

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    //场地类型 two科目二 three科目三
    type: {
      type: String,
      value: 'two'
    },
    data: {
      type: Object,
      value: {},
      observer(newVal) {
        if ('trainSiteTwoName' in newVal) {
          let category = ''
          let trainSiteName = ''
          let dist = ''
          let bus = newVal.trainSiteThreeNear?.bus
          let subway = newVal.trainSiteThreeNear?.subway
          let lat = ''
          let lng = ''
          let siteId = ''
          let addr = ''
          let addressDetail = ''
          let regionId =''
          let regionName =''
          if (this.properties.type === 'two') {
            categoryOptions.forEach(item => {
              if (Number(item.value) === Number(newVal.trainSiteTwoType)) {
                category = item.label
              }
            })
            trainSiteName = newVal.trainSiteTwoName
            let n = Number(newVal.trainSiteTwoDistance).toFixed(2)
            dist = Number(n) + 'km'
            bus = newVal.trainSiteTwoNear?.bus
            subway = newVal.trainSiteTwoNear?.subway
            lat = newVal.trainSiteTwoLat
            lng = newVal.trainSiteTwoLng
            siteId = newVal.trainSiteTwoId
            addr = newVal.trainSiteTwoAddressDetail.replace(/.+?(省|市|自治区|自治州|县|区)/g, '')
            addressDetail = newVal.trainSiteTwoAddressDetail
            regionId = newVal.trainTwoRegionId
            
          } else if (this.properties.type === 'three') {
            trainSiteName = newVal.trainSiteThreeName
            let n = Number(newVal.trainSiteThreeDistance).toFixed(2)
            dist = Number(n) + 'km'

            bus = newVal.trainSiteThreeNear?.bus
            subway = newVal.trainSiteThreeNear?.subway
            lat = newVal.trainSiteThreeLat
            lng = newVal.trainSiteThreeLng
            siteId = newVal.trainSiteThreeId
            addr = newVal.trainSiteThreeAddressDetail.replace(/.+?(省|市|自治区|自治州|县|区)/g, '')
            addressDetail = newVal.trainSiteThreeAddressDetail
            regionId = newVal.trainThreeRegionId
          }
          regionName = app.globalData.regionMap.get(regionId)
          this.setData({
            categoryName: category,
            targetName: trainSiteName,
            distance: dist,
            nearestStationName: (subway ? subway?.title : bus?.title) || '',
            trafficType: subway ? 'subway' : 'bus',
            latitude: Number(lat),
            longitude: Number(lng),
            id: siteId,
            address: addr,
            addressDetail,
            regionId,
            regionName
          })
        }
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    categoryName: '',
    distance: '0',
    address: '',
    addressDetail: '',
    nearestStationName: '', //最近站点名称
    trafficType: 'bus',//bus subway
    latitude: 0,
    longitude: 0,
    targetName: '',
    id: '',
    themeCode:'',
    regionId:'',
    regionName:''
  },
  lifetimes: {
    attached() {
      //从本地获取当前选择的城市编码
      this.setData({
        themeCode: wx.getStorageSync('themeCode')
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onOpenPage(res: any) {
      if (res.currentTarget.dataset.type === 'two') {
        wx.navigateTo({
          url: '../../pages/site/subjectTwo/subjectTwo?id=' + this.data.id+ '&name=' + this.data.targetName
        })
      } else if (res.currentTarget.dataset.type === 'three') {
        const userLocation = wx.getStorageSync('userLocation')
        console.log('userLocation', userLocation)
        if (userLocation) {
          wx.navigateTo({
            url: '../../pages/site/subjectThree/subjectThree?lat=' + this.data.latitude + '&lng=' + this.data.longitude + '&name=' + this.data.targetName + '&address=' + this.data.addressDetail
          })
        } else {
          const latitude = this.data.latitude
          const longitude = this.data.longitude
          wx.openLocation({
            latitude,
            longitude,
            scale: 14,
            name: this.data.targetName
          })
        }
      }
    },
    openMap() {
      const customOpenMapComponent = this.selectComponent('.customOpenMap')
      customOpenMapComponent.openMap({
        latitude: Number(this.data.latitude),
        longitude: Number(this.data.longitude),
        destination: this.data.targetName,
      })
    },
  }
})